package gov.pbc.nn.kjgl.train.view;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.view.widget.form.autoform.AutoFormElement;

import gov.pbc.nn.core.bdf.entity.Dept;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.Constants;
import gov.pbc.nn.kjgl.entity.TrainingPlan;
import gov.pbc.nn.kjgl.train.dao.TrainPlanSHDao;

@Component
public class TrainPlanSHPR {

	@Autowired
	private TrainPlanSHDao trainPlanSHDao;

	@DataProvider
	public void getAll(Page<TrainingPlan> page, Criteria criteria) {
		trainPlanSHDao.getAll(page, criteria);
	}

	@Expose
	@Transactional
	public void updateState(String opinion, TrainingPlan trainPlan, int cldsh, String type, String czName,
			String czUsername) {
		trainPlanSHDao.updateState(opinion, trainPlan, cldsh, type, czName, czUsername);
	}

	public void onInit(AutoFormElement ldsh) {
		Dept dept = (Dept) ContextHolder.getLoginUser().getDepts().get(0);
		List<IUser> ls = SecurityUtils.getUserByRoleForDeptWithChildren(dept.getCDept().getId(), Constants.ROLE_CZ);
		for (IUser u : ls) {
			if (ContextHolder.getLoginUserName().equals(u.getUsername())) {
				ldsh.setVisible(false);
				return;
			}
		}
	}
}
